预览:在我们开始制作我们的第一个程序之前,让我们看一个例子。

  • 该程序创建一个球和网,然后在它们之间画一条线。
  • 你被要求估计线的斜率。
  • 如果您的猜测很接近,请得分!如果没有......看球去哪里!实际斜率将显示在屏幕上。
  • 点击运行再试一次,然后提交接下来继续。

To navigate the page using the TAB key, first press ESC to exit the code editor.

#Set the grid and background stage.set_axis(100) stage.create_grid_overlay(10, "grey") stage.set_background("soccerfield") #Create coordinates for ball, create ball ballx = -90 bally = random.randint(-100, 100) ball = codesters.Sprite("soccerball", ballx, bally) #Create coordinates for net, create net netx = 80 nety = random.randint(-100, 100) net = codesters.Sprite("soccernet", 80,nety) net.flip_right_left() #Draw line between ball and net line = codesters.Line(-90, bally, 80, nety, "blue") slopeguess = float(line.ask("What is the slope of the line?")) #Calculate slope rise = nety-bally run = netx-ballx slope = rise/run #Display slope # sprite = codesters.Text("text", x, y) sprite = codesters.Text(slope, 0, 30) #Calculate new point for ball newrise = (slopeguess*run) endy = bally + newrise ball.glide_to(netx, endy)
  • Run Code
  • 提交作品
  • 下个活动
  • Show Console
  • Reset Code Editor
  • Codesters How To (opens in a new tab)